package com.eshop.modules.mp.service.impl;

import java.lang.invoke.SerializedLambda;
import java.io.Serializable;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.eshop.exception.EntityExistException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import java.io.IOException;
import java.util.Iterator;
import com.eshop.utils.FileUtil;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.eshop.common.utils.QueryHelpPlus;
import com.eshop.modules.mp.service.dto.WechatReplyDto;
import java.util.LinkedHashMap;
import java.util.List;
import com.github.pagehelper.PageInfo;
import java.util.Map;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.mp.service.dto.WechatReplyQueryCriteria;
import com.eshop.dozer.service.IGenerator;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.eshop.modules.mp.service.WechatReplyService;
import com.eshop.modules.mp.domain.WechatReply;
import com.eshop.modules.mp.service.mapper.WechatReplyMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = { Exception.class })
public class WechatReplyServiceImpl extends BaseServiceImpl<WechatReplyMapper, WechatReply> implements WechatReplyService
{
    private final IGenerator generator;
    
    public Map<String, Object> queryAll(final WechatReplyQueryCriteria criteria, final Pageable pageable) {
        this.getPage(pageable);
        final PageInfo<WechatReply> page = (PageInfo<WechatReply>)new PageInfo((List)this.queryAll(criteria));
        final Map<String, Object> map = new LinkedHashMap<String, Object>(2);
        map.put("content", this.generator.convert(page.getList(), (Class)WechatReplyDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }
    
    public List<WechatReply> queryAll(final WechatReplyQueryCriteria criteria) {
        return (List<WechatReply>)((WechatReplyMapper)this.baseMapper).selectList((Wrapper)QueryHelpPlus.getPredicate(WechatReply.class, criteria));
    }
    
    public void download(final List<WechatReplyDto> all, final HttpServletResponse response) throws IOException {
        final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (final WechatReplyDto yxWechatReply : all) {
            final Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put("关键字", yxWechatReply.getKey());
            map.put("回复类型", yxWechatReply.getType());
            map.put("回复数据", yxWechatReply.getData());
            map.put("0=不可用  1 =可用", yxWechatReply.getStatus());
            map.put("是否隐藏", yxWechatReply.getHide());
            list.add(map);
        }
        FileUtil.downloadExcel((List)list, response);
    }
    
    public WechatReply isExist(final String key) {
        final WechatReply wechatReply = (WechatReply)this.getOne((Wrapper) Wrappers.<WechatReply>lambdaQuery().eq(WechatReply::getKey, key));
        return wechatReply;
    }
    
    public void create(final WechatReply wechatReply) {
        if (this.isExist(wechatReply.getKey()) != null) {
            throw new EntityExistException((Class)WechatReply.class, "key", wechatReply.getKey());
        }
        this.save(wechatReply);
    }
    
    public void upDate(final WechatReply resources) {
        final WechatReply wechatReply = (WechatReply)this.getById((Serializable)resources.getId());
        final WechatReply wechatReply2 = this.isExist(resources.getKey());
        if (wechatReply2 != null && !wechatReply2.getId().equals(wechatReply.getId())) {
            throw new EntityExistException((Class)WechatReply.class, "key", resources.getKey());
        }
        wechatReply.copy(resources);
        this.saveOrUpdate(wechatReply);
    }
    
    public WechatReplyServiceImpl(final IGenerator generator) {
        this.generator = generator;
    }
}
